import 'package:flutter/material.dart';
import '../../models/serach_info_model.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'dart:io';

class CommentView extends StatelessWidget {
  final CommentArrayModel commentInfoModel;

  const CommentView({Key? key, required this.commentInfoModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 0, bottom: Platform.isIOS ? 10 : 40, left: 15, right: 15),
      padding: EdgeInsets.zero,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 标题 + 分数
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Padding(
                padding: const EdgeInsets.only(left: 15, top: 5),
                child: Text(
                  '${AppLocalizations.of(context)!.evaluate} (${commentInfoModel.page?.records.length ?? 0})',
                  style: const TextStyle(fontSize: 15, color: Color(0xFF2A2A2A)),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(right: 15, top: 5),
                child: Row(
                  children: [
                    Text(
                      AppLocalizations.of(context)!.score,
                      style: const TextStyle(fontSize: 13, color: Color(0xFF2A2A2A)),
                    ),
                    const SizedBox(width: 2),
                    Text(
                      commentInfoModel.score,
                      style: const TextStyle(
                        color: Color(0xFFF61F03),
                        fontSize: 20,
                        fontWeight: FontWeight.w900,
                        fontStyle: FontStyle.italic,
                      ),
                    ),
                    if ((commentInfoModel.page?.records.length ?? 0) > 1)
                      const SizedBox(width: 3),
                      const Icon(Icons.arrow_forward_ios, size: 12, color: Color(0xFF2C2C2C)),
                  ],
                ),
              ),
            ],
          ),

          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 15),
            child: Divider(color: Color(0xFFF5F5F5)),
          ),

          // 评论内容 或 暂无评论
          if ((commentInfoModel.page?.records.isEmpty ?? true))
            const Padding(
              padding: EdgeInsets.symmetric(vertical: 15),
              child: Center(
                child: Text(
                  '暂无评论',
                  style: TextStyle(fontSize: 15, color: Color(0xFF666666)),
                ),
              ),
            )
          else
            CommentCell(comment: commentInfoModel.page!.records[0]),
        ],
      ),
    );
  }
}

class CommentCell extends StatelessWidget {
  final CommentListModel comment;

  const CommentCell({Key? key, required this.comment}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 6.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 20, bottom: 10),
            child: Row(
              children: [
                Image.asset(
                  'assets/images/默认头像.png',
                  width: 33,
                  height: 33,
                  fit: BoxFit.cover,
                ),
                const SizedBox(width: 10),
                Expanded(
                  child: Text(
                    comment.name,
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 15,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                RatingBar.builder(
                  initialRating: comment.score,
                  minRating: 0,
                  direction: Axis.horizontal,
                  allowHalfRating: true,
                  itemCount: 5,
                  itemSize: 25,
                  itemPadding: EdgeInsets.zero,
                  itemBuilder: (context, _) => const Icon(
                    Icons.star,
                    color: Colors.amber,
                  ),
                  unratedColor: Colors.grey[300],
                  onRatingUpdate: (_) {},
                  ignoreGestures: true,
                ),
                const SizedBox(height: 8),
                Text(
                  comment.msg,
                  style: const TextStyle(
                    fontSize: 14,
                    color: Color(0xFF000000),
                    height: 1.5,
                  ),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
                const SizedBox(height: 8),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
